#ifndef _IOT_SOCKET_H_
#define _IOT_SOCKET_H_

typedef void(* iot_socketEventCallBackFuncPtr)(iot_int8_t soc_id,iot_uint8_t event,iot_bool_t result);	
typedef void(* iot_getHostByNameCallBackFuncPtr)(iot_bool_t result,iot_uint8_t * addr,iot_uint8_t addr_len);

typedef struct 
{
	iot_uint8_t	addr[4];
    iot_uint16_t port;
} iot_S_SocketAddr;

typedef enum
{
    IOT_SOC_SOCK_STREAM = 0,  /* stream socket, TCP */
    IOT_SOC_SOCK_DGRAM,       /* datagram socket, UDP */
    IOT_SOC_SOCK_SMS,         /* SMS bearer */
    IOT_SOC_SOCK_RAW          /* raw socket */
} iot_E_SocketType;

typedef enum
{
    IOT_SOC_OOBINLINE     = 0x01 << 0,  /* not support yet */
    IOT_SOC_LINGER        = 0x01 << 1,  /* linger on close */
    IOT_SOC_NBIO          = 0x01 << 2,  /* Nonblocking */
    IOT_SOC_ASYNC         = 0x01 << 3,  /* Asynchronous notification */   

    IOT_SOC_NODELAY       = 0x01 << 4,  /* disable Nagle algorithm or not */
    IOT_SOC_KEEPALIVE     = 0x01 << 5,  /* enable/disable the keepalive */
    IOT_SOC_RCVBUF        = 0x01 << 6,  /* set the socket receive buffer size */
    IOT_SOC_SENDBUF       = 0x01 << 7,  /* set the socket send buffer size */

    IOT_SOC_NREAD         = 0x01 << 8,  /* no. of bytes for read, only for soc_getsockopt */
    IOT_SOC_PKT_SIZE      = 0x01 << 9,  /* datagram max packet size */
    IOT_SOC_SILENT_LISTEN = 0x01 << 10, /* SOC_SOCK_SMS property */
    IOT_SOC_QOS           = 0x01 << 11, /* set the socket qos */

    IOT_SOC_TCP_MAXSEG    = 0x01 << 12, /* set the max segmemnt size */
    IOT_SOC_IP_TTL        = 0x01 << 13, /* set the IP TTL value */
    IOT_SOC_LISTEN_BEARER = 0x01 << 14, /* enable listen bearer */
    IOT_SOC_UDP_ANY_FPORT = 0x01 << 15, /* enable UDP any foreign port */

    IOT_SOC_WIFI_NOWAKEUP = 0x01 << 16, /* send packet in power saving mode */
    IOT_SOC_UDP_NEED_ICMP = 0x01 << 17, /* deliver NOTIFY(close) for ICMP error */
    IOT_SOC_IP_HDRINCL    = 0x01 << 18,  /* IP header included for raw sockets */
    IOT_SOC_IPSEC_POLICY      = 0x01 << 19, /* ip security policy */
    IOT_SOC_TCP_ACKED_DATA  = 0x01 << 20,  /* TCPIP acked data */
    IOT_SOC_TCP_DELAYED_ACK = 0x01 << 21, /* TCP delayed ack */
    IOT_SOC_TCP_SACK        = 0x01 << 22, /* TCP selective ack */
    IOT_SOC_TCP_TIME_STAMP  = 0x01 << 23,  /* TCP time stamp */
    IOT_SOC_TCP_ACK_MSEG  = 0x01 << 24   /* TCP ACK multiple segment */
} iot_E_SocketOption;

/* event */
typedef enum
{
    IOT_SOC_READ    = 0x01,  /* Notify for read */
    IOT_SOC_WRITE   = 0x02,  /* Notify for write */
    IOT_SOC_ACCEPT  = 0x04,  /* Notify for accept */
    IOT_SOC_CONNECT = 0x08,  /* Notify for connect */
    IOT_SOC_CLOSE   = 0x10   /* Notify for close */
}iot_E_SocketEvent;

typedef enum
{
    IOT_SOC_SUCCESS           = 0,     /* success */
    IOT_SOC_ERROR             = -1,    /* error */
    IOT_SOC_WOULDBLOCK        = -2,    /* not done yet */
    IOT_SOC_LIMIT_RESOURCE    = -3,    /* limited resource */
    IOT_SOC_INVALID_SOCKET    = -4,    /* invalid socket */
    IOT_SOC_INVALID_ACCOUNT   = -5,    /* invalid account id */
    IOT_SOC_NAMETOOLONG       = -6,    /* address too long */
    IOT_SOC_ALREADY           = -7,    /* operation already in progress */
    IOT_SOC_OPNOTSUPP         = -8,    /* operation not support */
    IOT_SOC_CONNABORTED       = -9,    /* Software caused connection abort */
    IOT_SOC_INVAL             = -10,   /* invalid argument */
    IOT_SOC_PIPE              = -11,   /* broken pipe */
    IOT_SOC_NOTCONN           = -12,   /* socket is not connected */
    IOT_SOC_MSGSIZE           = -13,   /* msg is too long */
    IOT_SOC_BEARER_FAIL       = -14,   /* bearer is broken */
    IOT_SOC_CONNRESET         = -15,   /* TCP half-write close, i.e., FINED */
    IOT_SOC_DHCP_ERROR        = -16,   /* DHCP error */
    IOT_SOC_IP_CHANGED        = -17,   /* IP has changed */
    IOT_SOC_ADDRINUSE         = -18,   /* address already in use */
    IOT_SOC_CANCEL_ACT_BEARER = -19    /* cancel the activation of bearer */
} iot_E_SocketErrorCode;

extern void iot_setSocketEventCallBackFunc(iot_int8_t soc_id, iot_socketEventCallBackFuncPtr funcPtr);
extern iot_int8_t iot_socketCreate(iot_E_SocketType type);
extern iot_int8_t iot_socketSetOpt(iot_int8_t soc_id, iot_uint32_t option, void *val, iot_uint8_t val_size);
extern iot_int8_t iot_socketConnect(iot_int8_t soc_id, iot_S_SocketAddr *addr);
extern iot_int32_t iot_socketSend(iot_int8_t soc_id, void *buf, iot_int32_t len);
extern iot_int32_t iot_socketRecv(iot_int8_t soc_id, void *buf, iot_int32_t len);
extern iot_int8_t iot_socketClose(iot_int8_t soc_id);
extern iot_int8_t iot_socketGetHostByName(iot_getHostByNameCallBackFuncPtr ptr, const iot_int8_t *domain_name,iot_uint8_t *addr,iot_uint8_t *addr_len);

#endif
